<template>
  <component
      v-if="current!=null"
      :is="current"
      :formJson="formJson"
      :formConfig="formConfig"
      :formData="formData"
      :optionData="optionData"
      :templateInfo="templateInfo"
      :apiType="apiType"
      :device="device"
  />
</template>

<script>
import {UserCommitApi} from "@/api/user_common";
import {BizTypeEnum} from "@/enums/bizTypeEnum";
import {AndroidSDK} from "@/sdk/AndroidSdkService";
import {getQueryParam} from '@/utils/common'

export default {
  name: "TempView",

  components: {
    RoomScreen: () => import("./scenes/room_screen.vue"),
    WaitingScreen: () => import("./scenes/waiting_screen.vue"),
    WindowScreen: () => import("./scenes/window_screen.vue"),
    InformationScreen: () => import("./scenes/information_screen.vue"),
    MachineScreen: () => import("./scenes/machine_screen.vue"),
    NumberMachineScreen: () => import("./scenes/number_machine_screen.vue")
  },

  data() {
    return {
      // 当前组件
      current: null,
      formJson: {
        widgetList: [],
        formConfig: {
          devType: true
        },
      },
      formConfig: {},
      formData: {},
      optionData: {},
      modelId: null,
      templateInfo: null,
      // 0表示门诊 1 第三方 2 其他
      apiType: 0,
      device: "Android",
      hrefInfo: getQueryParam(),
    };
  },

  mounted() {
    // 初始化参数
    this.initParams();
    // 获取模板信息
    this.getModelInfo();
    // 监听请求地址的变化
    window.addEventListener('popstate', this.handlePopstate);
  },

  methods: {
    /**
     * 接收路由参数
     */
    async initParams() {
      const searchParams = this.$route.query;
      console.log(searchParams)
      if (searchParams.modelId) {
        this.modelId = searchParams.modelId;
      }
      if (searchParams.devType) {
        const devType = searchParams.devType;
        this.formConfig.devType = parseInt(devType) !== 2;
      } else {
        this.formConfig.devType = true;
      }

      // 环境
      if (searchParams.device) {
        this.device = searchParams.device;
      }
      // 设置环境
      AndroidSDK.environment = this.device;
    },

    /**
     * 获取模板数据
     */
    async getModelInfo() {
      let resultInfo = Object();
      try {
        let formData = {
          messageId: this.modelId,
          informationType: "MODEL",
        };
        const {code, data} = await UserCommitApi.getTemplateDetail(formData);
        if (200 === code) {
          this.templateInfo = data;
          // 设备绑定模板
          Reflect.set(resultInfo, 'state', true);
          Reflect.set(resultInfo, "result", data);
          const {apiType, widgetList, formConfig} = data;
          // 设置接口类型
          this.apiType = apiType;
          let devType = this.formJson.formConfig.devType;
          if (widgetList) {
            this.formJson.widgetList = JSON.parse(widgetList);
            this.formJson.formConfig = JSON.parse(formConfig);
            this.formJson.formConfig.devType = devType;
          }
          // 跳转对应主视图
          this.gotoMainView(this.templateInfo.devType);
        } else {
          Reflect.set(resultInfo, 'state', false);
        }
      } catch (e) {
        console.log(e)
        Reflect.set(resultInfo, 'state', false);
      }
      await this.$store.dispatch('setDevTemplateInfo', resultInfo);
    },

    /**
     * 去目标视图
     * @param devType
     */
    gotoMainView(devType) {
      let {formConfig} = this.formJson;
      this.current = null;
      if (formConfig?.parentInfo?.compBiz) {
        const params = this.$route.query;
        this.$router.push({
          path: `/DRCbizList?modelId=${params.modelId}&devType=${params.devType}&t=${this.$day().unix()}`
        });
      } else {
        this.current = BizTypeEnum[devType + ""].value;
      }
    },

    /**
     * 地址发生变化的副影响
     */
    handlePopstate() {
      let temp = getQueryParam();
      if (!this.$lodash.isEqual(this.hrefInfo, temp)) {
        this.hrefInfo = temp;
        this.$router.go();
      }
    }

  },

  beforeDestroy() {
    // 移除事件监听
    window.removeEventListener('popstate', this.handlePopstate);
  },

  // watch: {
  //   // 获取配置项
  //   $route: {
  //     handler: function (val) {
  //       // 重新加载 防止缓存
  //       this.$router.go()
  //     },
  //     deep: true,
  //     immediate: false
  //   },
  // },
};
</script>

